﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class MessagePanel : BasePanel
{
    private Text message;
    private float showTime = 1f;
    private string messageStr = null;

    private void Start()
    {
        message = GetComponentInChildren<Text>();
        message.enabled = false;
    }

    private void Update()
    {
        if (messageStr != null)
        {
            ShowMessage(messageStr);
            messageStr = null;
        }
    }

    public override void OnEnter()
    {
        base.OnEnter();
        uiMgr.InjectMessagePanel(this);
    }

    /// <summary>
    /// 异步显示
    /// </summary>
    /// <param name="msg"></param>
    public void ShowMessageSync(string msg)
    {
        messageStr = msg;
    }

    public void ShowMessage(string msg)
    {
        message.CrossFadeAlpha(1f, .2f, false);
        message.text = msg;
        message.enabled = true;
        Invoke("OnHide", showTime);
    }

    /// <summary>
    /// 隐藏
    /// </summary>
    private void OnHide()
    {
        message.CrossFadeAlpha(0f, 1f, false);
    }
}
